<?php

namespace app\controller;

use app\BaseController;
use app\Request;
use app\service\BigService;
use app\service\ImageService;
use mysql_xdevapi\Exception;
use think\facade\Log;


class Indexpc extends BaseController
{
    protected $bigServiec;
    protected $r; //Request 对象

    protected $imageService;

    public function __construct(BigService $bigServiec, Request $r)
    {
        $this->bigServiec = $bigServiec;
        $this->r = $r;
    }


    //万能表单的设置 要再写一个方法包装下
    public function findformsetbypidandaid(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr1 = $this->bigServiec->findformsetbypidandaid($pid, $aid);
        //return json_encode($arr1);
        $len = count($arr1);
        if ($len > 0) {
            $item = $arr1[0];
            //return json_encode($item);
            $value = $item['value'];  // mediumtext NOT NUL
            $r['has'] = 'yes';
            $r['data'] = unserialize($value); // 要不要 $tt 接受下无所谓
            //要的是needset --是个arr
            if (is_array($r['data'])) {
                $r['data_type'] = 'php_array';
            }
            return json_encode($r);
        } else {
            $r['has'] = 'no';
            $r['msg'] = "没有数据";
            return json_encode($r);
        }
    }

    //从theme表中获取一些值。等待测试。因为 业务接口需要
    public function findthemeinfobypidaid(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr1 = $this->bigServiec->findthemeinfobypidaid($pid, $aid);
        $len = count($arr1);
        if ($len > 0) {
            $r['has'] = 'yes';
            $r['data'] = $arr1[0];
        } else {
            $r['has'] = 'no';
            $r['msg'] = "没有数据";

        }
        return json_encode($r);
    }

    //findshopsopt,回显新老生
    public function findshopsopt(Request $request)
    {

        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr1 = $this->bigServiec->findconfigbypidaid($pid, $aid);
        $len = count($arr1);
        if ($len > 0) {
            $item = $arr1[0];
            //return json_encode($item);
            $value = $item['value'];  // mediumtext NOT NUL   也是value
            $r['has'] = 'yes';
            $uv = unserialize($value); // 要不要 $tt 接受下无所谓
            if (is_array($uv)) {
                $r['data_type'] = 'php_array';
                $r['shops']= $uv['shops'];
                $r['options']= $uv['options'];
            }

        } else {
            $r['has'] = 'no';
            $r['msg'] = "没有数据";
        }
        return json_encode($r);
    }


    //findconfigbypidaid()
    public function findconfigbypidaid(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr1 = $this->bigServiec->findconfigbypidaid($pid, $aid);
        $len = count($arr1);
        if ($len > 0) {
            $item = $arr1[0];
            //return json_encode($item);
            $value = $item['value'];  // mediumtext NOT NUL   也是value
            $r['has'] = 'yes';
            //$r['data'] = unserialize($value); // 要不要 $tt 接受下无所谓
            $big = unserialize($value);
            $smallarr['shops'] = $big['shops'];
            $smallarr['options']=$big['options'];
            $r['data']=$smallarr;
            if (is_array($r['data'])) {
                $r['data_type'] = 'php_array';
            }
        } else {
            $r['has'] = 'no';
            $r['msg'] = "没有数据";
        }
        return json_encode($r);
    }

    //pcfindbaseinfo
    public function pcfindbaseinfo(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr = $this->getinfofromtable($pid, $aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        $pcdata = [];
        if (!is_null($arr)) {
            $r['has'] = 'yes';
            $pcdata['title'] = $arr['title'];
            $pcdata['mobile'] = $arr['mobile'];
            $arr2 = $this->getfinofromconfig($pid, $aid);
            if (!is_null($arr2)) {
                if (is_array($arr2)) {
                    $r['data_type2'] = 'php_array2';
                    $pcdata['jubf'] = $arr2['jubf'];  //增补的
                }
            }
            $r['pcdata'] = $pcdata;
            //$arr['jubf22'] ="厉害22"; //写在后面没有用
            return json_encode($r);
        }
        $r['has'] = 'no';
        $r['msg'] = '没有数据';
        return json_encode($r);
    }

    //pcfindshareinfo
    //分享的一些设置
    public function pcfindshareinfo(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        //要不要翻着来一下 arr1是config  arr2是table
        $arr1 = $this->getfinofromconfig($pid, $aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        $pcdata = [];
        if (!is_null($arr1)) {
            $r['has'] = 'yes';
            $pcdata['sharetitle1'] = $arr1['sharetitle1'];
            $pcdata['sharetitle2'] = $arr1['sharetitle2'];

            $arr2 = $this->getinfofromtable($pid, $aid);
            if (!is_null($arr2)) {
                if (is_array($arr2)) {
                    $r['data_type2'] = 'php_array2';
                    $pcdata['img'] = $arr2['img'];  //增补的
                    $pcdata['begintime']=$arr2['begintime'];
                    $pcdata['endtime']=$arr2['endtime'];
                    $pcdata['status']=$arr2['status'];
                }
            }
            $r['pcdata'] = $pcdata;
            //$arr['jubf22'] ="厉害22"; //写在后面没有用
            return json_encode($r);
        }
        $r['has'] = 'no';
        $r['msg'] = '没有数据';
        return json_encode($r);
    }

    //pcfindptinfo 拼团设置
    public function pcfindptinfo(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid, $aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            $pcdata['payfee'] = $arr_config['payfee'];  //增补的
            $pcdata['payfee_txt'] = "开团金额";
            $pcdata['joinfee'] = $arr_config['joinfee'];  //增补的
            $pcdata['joinfee_txt'] = "参团金额";
            $pcdata['maxcount'] = $arr_config['maxcount'];  //增补的
            $pcdata['maxcount_txt'] = "团员人数";  //增补的
            $pcdata['groupsum'] = $arr_config['groupsum'];  //增补的
            $pcdata['groupsum_txt'] = "活动总团数";  //增补的

            //合并 headstyle
            $pcdata['headstyle'] = $arr_config['headstyle'];  //增补的


            $r['pcdata'] = $pcdata;
            return json_encode($r);
        }
        $r['msg'] = '没有数据';
        return json_encode($r);
    }


    //todo dm pmd
    public function pcfinddmpmd(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_set = $this->getfinfofromset($pid,$aid);
        $r = ['has' => 'no', 'pcdata' => null, 'data_type' => null];
        $pcdata = [];
        if (!is_null($arr_set)) {
            $r['has'] = 'yes';
            //todo work
            $pcdata = $arr_set;  //重量级数据。 要问陈鑫
            $r['pcdata'] = $pcdata;
            return json_encode($r);
        }
        return json_encode($r);
    }

    //pcfindthemepic 来自config
    public function pcfindthemepic(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = ['has' => 'no', 'pcdata' => null, 'data_type' => null];
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            //todo work
            $pcdata['headlogo'] = $arr_config['headlogo'];
            $pcdata['centerimgs'] = $arr_config['centerimgs'];

            $r['pcdata'] = $pcdata;
            $r['cankao'] = $arr_config;
            return json_encode($r);
        }
        return json_encode($r);
    }

    //todo 小卢说把 接口合在一起
    public function pcfindbigpage(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        //没有theme 全是config 和 wn_set
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = ['has' => 'no', 'pcdata' => null, 'data_type' => null];
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';

            //第一组key 页面模板选择 打平
            $pcdata['headstyle']=$arr_config['headstyle'];

            //第二组
            $arr_set  = $this->getfinfofromset($pid,$aid);
            if (!is_null($arr_set)) {
                $pcdata['wn_set']= $arr_set;
                $pcdata['dm']= $arr_set;
                $pcdata['pmd']= $arr_set;
                $pcdata['vtop'] = $arr_set;
            }else{
                $pcdata['dm']= null;
                $pcdata['pmd']= null;
                $pcdata['vtop'] = null;
            }

            //第三组 倒计时
            $pcdata['ctime']='just pc';  //倒计时是否未开
            $pcdata['ctlist']='just pc'; //排行榜是否开

            //第四组
            //代码需要挪到 上面了。

            //第五组 活动图片
            $pcdata['headlogo'] = $arr_config['headlogo'];
            $pcdata['centerimgs'] = $arr_config['centerimgs'];

            $r['pcdata'] = $pcdata;
            return json_encode($r);
        }

        return json_encode($r);
    }






    //    //pcfindredall  config
    public function pcfindredall(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = ['has' => 'no', 'pcdata' => null, 'data_type' => null];
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            //todo work
            //$pcdata['headlogo'] = $arr_config['headlogo'];
            $pcdata = $arr_config;

            $r['pcdata'] = $pcdata;

            return json_encode($r);
        }
        return json_encode($r);
    }


    //pcfindbutton 按钮 to小卢
    public function pcfindbutton(Request $request){
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = ['has' => 'no', 'pcdata' => null, 'data_type' => null];
        $pcdata = [];
        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            //todo work
            //$pcdata['headlogo'] = $arr_config['headlogo'];
            //$pcdata = $arr_config;
            $pcdata['lablegroup'] = $arr_config['lablegroup'];
            //$pcdata['lablegroup_txt'] = '开团按钮1';

 //           $pcdata['lablepygroup'] = $arr_config['lablepygroup'];
//            $pcdata['lablepygroup_txt'] = '开团按钮2';


            $pcdata['lablecjgroup'] = $arr_config['lablecjgroup'];
            //$pcdata['lablecjgroup_txt'] = '参团按钮';

            $pcdata['lablesuccess'] = $arr_config['lablesuccess'];
            //$pcdata['lablesuccess_txt'] = '组团成功';




            $arr_set  = $this->getfinfofromset($pid,$aid);
            if (!is_null($arr_set)) {
                $pcdata['wn_set']= $arr_set;
            }else{
                $pcdata['wn_set']= null;
            }

            $r['pcdata'] = $pcdata;


            return json_encode($r);
        }
        return json_encode($r);
    }

    //pcfindmakedata
    public function pcfindmakedata(Request $request)
    {
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_table = $this->getinfofromtable($pid, $aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        $pcdata = [];
        if (!is_null($arr_table)) {
            $r['has'] = 'yes';
            $pcdata['scansum'] = $arr_table['scansum'];
            $pcdata['fanssum'] = $arr_table['fanssum'];
            $pcdata['groupsum'] = $arr_table['groupsum'];
            $pcdata['paynum'] = $arr_table['paynum'];

        }
        $arr_set = $this->getfinfofromset($pid,$aid);
        if (!is_null($arr_set)) {
            $r['has2'] = 'yes'; //调用方可能要注意了
            $pcdata['wn_set']= $arr_set;
        }else{
            $r['has2'] = 'no'; //调用方可能要注意了
            $pcdata['dm']= null;
            $pcdata['pmd']= null;
            $pcdata['vtop'] = null;
        }
        $r['pcdata'] = $pcdata;
        return json_encode($r);
    }



    public function getinfofromtable($pid, $aid)
    {
        $arr1 = $this->bigServiec->findthemeinfobypidaid($pid, $aid);
        $len = count($arr1);
        if ($len > 0) {
            return $arr1[0];
        }
        return null;
    }

    //
    public function getfinofromconfig($pid, $aid)
    {
        $arr1 = $this->bigServiec->findconfigbypidaid($pid, $aid);
        $len = count($arr1);
        if ($len > 0) {
            $item = $arr1[0];
            //return json_encode($item);
            $value = $item['value'];  // mediumtext NOT NUL   也是value
            return unserialize($value); //
        }
        return null;
    }

    //小程序的万能表单
    public function getfinfofromset($pid, $aid){
        $arr_set = $this->bigServiec->findformsetbypidandaid($pid, $aid);
        $len = count($arr_set);
        if ($len > 0) {
            $item = $arr_set[0];
            $value = $item['value'];  // mediumtext NOT NUL
            return unserialize($value);
        }
        return null;
    }

    //实际是交易数
    //pcfindorder
    public function pcfindordernum1($lgdate){

        $arr1 = $this->bigServiec->pcfindordernum($lgdate);
        //todo $lgdate 换成 @lgdate2
        return $arr1;
        $len = count($arr1);
        if ($len > 0) {
            return '没有数据';
        }
        return json_encode($arr1);
    }

    //直接用这个 pcworkergroup
    //java


    //员工分组回想
    public function pcfindgroup(Request $request){
        //我知道了
        $pid = $request->get("pid");
        $aid = $request->get("aid");
        $arr_config = $this->getfinofromconfig($pid,$aid);
        $r = [
            'has' => 'no',
            'pcdata' => null,
            'data_type' => null
        ];
        $pcdata = [];

        if (!is_null($arr_config)) {
            $r['has'] = 'yes';
            $pcdata['work_group'] = $arr_config['work_group'];  //增补的

            $r['pcdata'] = $pcdata;
            return json_encode($r);
        }
        $r['msg'] = '没有数据';
        return json_encode($r);
    }

    //---------------接受支付成功通知--------
    public function payfznotify(Request $request){
        //来自java的支付成功通知
        $og = $request->post();
        //$cmzgpid = $og['cmzgPid'];
        //$cmzgAid = $og['cmzgAid'];
        //$cmzgActivityType =$og['cmzgActivityType'];
        //直接
        //todo 会不会报错
        //$newflagbuy = input('post.newflagbuy', "no"); // 第二个参数是默认值

        $log =json_encode($request->post());
        Log::info("记录支付payfznotify===$log");
        //Log::info("newflagbuy ===$newflagbuy");
        $arr['flag']=3;
        return json_encode($arr);
    }

    //todo 记录mppay的入参日志todo 1
    //todo 记录支付回调的控制器入参。


    public function xiav1(Request $request){
//        //夏天要一个解析的 东东
//        $id = $request->get("id");
//        $arr1 = $this->bigServiec->xiav1($id);
//        //$v = $arr1['value'];
//        $len = count($arr1);
//        //$r['len']=$len;
//        $vstr_row = $arr1[0];
//        $s_v =$vstr_row['value'];
//        try{
//
//            $setting = unserialize($s_v);
//            $r['setting']=$setting;
//        } catch (\Exception $e) {
//
//        //$r['vstr_row']= $vstr_row;
//        return json_encode($r);
    }


    public function gong_info3(Request $request)
    {
        $arr = $request->param();
        Log::info("cx-gong_info3".json_encode($arr));

        $appid = $request->get('appid');
        if (empty($appid)) {
            $appid = 'wxbbce02401f0a2f6d';
        }
        $appsecret = $request->get('appsecret');
        if (empty($appsecret)) {
            $appsecret = '0315hK1w37DBO536Ih3w3K0MIB25hK1n';
        }
        $code = $request->get('code');
        if (!$code) {
            die('授权失败，缺少 code');
        }

// 请求微信接口获取 access_token 和 openid
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?" .
            "appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";

        $response = file_get_contents($url);
        Log::info("gong_info3==".$response);
        return $response;
       // $data = json_decode($response, true);
        return ;
//        if (isset($data['openid'])) {
//            $openid = $data['openid'];
//            echo "用户的 OpenID 是：{$openid}";
//        } else {
//            echo "获取 openid 失败：" . $data['errmsg'];
//        }
        //return View::fetch('info2');
    }

    public function gong_info4(Request $request)
    {
        $arr = $request->param();
        Log::info("cx-gong_info4".json_encode($arr));
        $access_token = $request->get('access_token');
        $openid = $request->get('openid');
        // 请求微信接口获取 access_token 和 openid
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
        $response = file_get_contents($url);
        Log::info("gong_info4==".$response);
        return $response;
    }


    public function t_find_f_name(Request $request){
        Log::info("t_find_fname".json_encode($request->param()));
        $pid = $request->get('pid');
        $aid = $request->get('aid');
        $openid = $request->get('openid');
       // return json_encode($request->param());
        $a = $this->find_f_name($pid,$aid,$openid);
        if(is_array($a)){
            //return 'arr';
        }

        $user = $a->first();
        $nickname = $user['nickname'];
        return $nickname;
//
//        if($a == null){
//            return 'wu';
//        }
//        return json_encode($a);
    }

    //找直推人姓名
    private function find_f_name($uniacid,$themeid,$f_openid)
    {

        $arr = $this->bigServiec->find_f_name($uniacid,$themeid,$f_openid);  //上线的时候  可以去掉
        return $arr;
    }




}